<?php

/**
 * alert actions.
 *
 * @package    student_manager
 * @subpackage alert
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class alertActions extends autoalertActions
{
public function executeRedirect(){
    //$user = new sfGuardUser();
    $user =  $this->getUser()->getGuardUser();
    
    if ($user->hasGroup('Administrator')){
        $this->redirect('en/users');
    }   
    
//    if ($user->hasGroup('Project Engineer')){
//        $this->redirect('bom/list');
//    }   
//    
//    if ($user->hasGroup('Store Keeper')){
//        $this->redirect('component/viewWarehouse');
//    }
//    
//    if ($user->hasGroup('Project Head')){
//        $this->redirect('mr2/list');
//    }
//    
//    if ($user->hasGroup('Support Head')){
//        $this->redirect('approval/ListApprovalObject?authenticationMain=APPROVE_PO&authenticationSub=PR_APPROVE_PO&mod=PO&backURL=');
//    }
//    
//    if ($user->hasGroup('General Director')){
//        $this->redirect('approval/ListApprovalObject?authenticationMain=APPROVE_MRMAIN&authenticationSub=PR_APPROVE_MRMAIN&mod=MR&backURL=');
//    }
//    
//    if ($user->hasGroup('L&W Manager')){
//        $this->redirect('component/viewWarehouse');
//    }
//    
//    if ($user->hasGroup('Purchaser')){
//        $this->redirect('po/list');
//    }
//    
//    if ($user->hasGroup('Production Manager')){
//        $this->redirect('approval/ListApprovalObject?authenticationMain=APPROVE_MRSUB&authenticationSub=PR_APPROVE_MRSUB&mod=MRSUB&backURL=');
//    }
//    
//    if ($user->hasGroup('Production Staff')){
//        $this->redirect('mr3/list');
//    }
 /*
Project Head 		
Store Keeper 	
Project Engineer
Support Head 	
General Director
L&W Manager 		
Purchaser 	
Account Head 		
Production Manager 	
Production Staff
*/

    $this->redirect('/main/showDashboard');
}

 public function executeEdit()
  {
    $this->alert = $this->getAlertOrCreate();

    if ($this->getRequest()->isMethod('post'))
    {
      $this->updateAlertFromRequest();

      try
      {
        $this->saveAlert($this->alert);
      }
      catch (PropelException $e)
      {
        $this->getRequest()->setError('edit', 'Could not save the edited Alerts.');
        return $this->forward('alert', 'list');
      }

      $this->getUser()->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('alert/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('alert/list');
      }
      else
      {
        return $this->redirect('alert/edit?id='.$this->alert->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }
    public function executeAdd(){
  	// Update alert information . 
  	
  }
  public function executeDeleteAlert(){
  	$alert = AlertPeer::retrieveByPK($this->getRequestParameter('alert')); 
  	$alert->delete(); 
  	switch($this->getRequestParameter('type')){
  		case CommonValues::STUDENT :
  			$this->redirect('student/edit?id='.$this->getRequestParameter('id')); 
  			break;
  		case CommonValues::FOLLOW_UP :
  			$this->redirect('follow_up/edit?id='.$this->getRequestParameter('id')); 
  			break;
  		case CommonValues::PROSPECT :
  			$this->redirect('potentiality/edit?id='.$this->getRequestParameter('id')); 
  	}
  	
  }
  
   public function executeList()
  {
    $this->executeRedirect();
  
    $this->processSort();
    $this->processFilters();
    
    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/alert/filters');
    // pager
    $this->pager = new sfPropelPager('Alert', 20);
    $c = new Criteria();
    $c->add(AlertPeer::OWNER_ID , $this->getUser()->getGuardUser()->getId()); 
    $c->add(AlertPeer::ALERT_DATE , date('Y-m-d'), Criteria::LESS_EQUAL ); 
    $c->add(AlertPeer::STATUS , CommonValues::NOT_COMPLETE ); 
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/alert')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/alert');
    }
  }
  
    protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['alert_date_is_empty']))
    {
      $criterion = $c->getNewCriterion(AlertPeer::ALERT_DATE, '');
      $criterion->addOr($c->getNewCriterion(AlertPeer::ALERT_DATE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['alert_date']))
    {
      if (isset($this->filters['alert_date']['from']) && $this->filters['alert_date']['from'] !== '')
      {
        $criterion = $c->getNewCriterion(AlertPeer::ALERT_DATE, date('Y-m-d', $this->filters['alert_date']['from']), Criteria::GREATER_EQUAL);
      }
      if (isset($this->filters['alert_date']['to']) && $this->filters['alert_date']['to'] !== '')
      {
        if (isset($criterion))
        {
          $criterion->addAnd($c->getNewCriterion(AlertPeer::ALERT_DATE, date('Y-m-d', $this->filters['alert_date']['to']), Criteria::LESS_EQUAL));
        }
        else
        {
          $criterion = $c->getNewCriterion(AlertPeer::ALERT_DATE, date('Y-m-d', $this->filters['alert_date']['to']), Criteria::LESS_EQUAL);
        }
      }

      if (isset($criterion))
      {
        $c->add($criterion);
      }
    }
    if (isset($this->filters['alert_type_is_empty']))
    {
      $criterion = $c->getNewCriterion(AlertPeer::ALERT_TYPE, '');
      $criterion->addOr($c->getNewCriterion(AlertPeer::ALERT_TYPE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['alert_type']) && $this->filters['alert_type'] !== '')
    {
      $c->add(AlertPeer::ALERT_TYPE, $this->filters['alert_type']);
    }
    
  }
  
}
